package com.csj.mvvmtest.ui.fourth

import android.content.DialogInterface
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableArrayMap
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.csj.mvvm.App
import com.csj.mvvm.api.interceptor.BearInterceptor
import com.csj.mvvm.api.retrofit.RetrofitClient2
import com.csj.mvvm.ui.base.BaseFragment2
import com.csj.mvvm.ui.base.BaseViewModel
import com.csj.mvvm.ui.main.ImageActivity
import com.csj.mvvm.utils.*
import com.csj.mvvm.widget.PopupWindowFix
import com.csj.mvvm.xml.AppTheme
import com.csj.mvvm.xml.Themes
import com.csj.mvvmtest.R
import com.csj.mvvmtest.adapter.DemoAdapter
import com.csj.mvvmtest.bean.NaviBean
import com.csj.mvvmtest.databinding.FragmentFourBinding
import com.csj.mvvmtest.databinding.PopupDemoBinding
import com.csj.mvvmtest.ui.MainActivity
import com.csj.mvvmtest.ui.second.SecondLeftAdapter

class FourthFragment : BaseFragment2<FourthModel, FragmentFourBinding>() {

    private var leftList: ArrayList<String> = arrayListOf("1", "2", "3", "3", "3", "4", "5", "6")
    private lateinit var adapter: DemoAdapter

    override fun getLayoutId(): Int {
        return R.layout.fragment_four
    }

    override fun initVM() {
        vm.tokenData.observe(this) {
            BearInterceptor.access_token = it.access_token!!
        }
    }

    override fun initClick() {
        v.ll1.setOnClickListener {
            val night = NightModeUtil.getNightMode()

            CustomDialog.Builder(mContext).setView().setMessage("新的设置需要重启应用")
                .setCancelable(false).setPositiveButton { dialog: DialogInterface, _ ->
                    dialog.cancel()
                    v.tv1.text = if (!night) {
                        "是"
                    } else {
                        "否"
                    }
                    NightModeUtil.setNightMode(!night)
                    android.os.Handler(Looper.getMainLooper()).postDelayed({
                        LogUtil.e("getNightMode=" + NightModeUtil.getNightMode())
                        NightModeUtil.restartApp(
                            mContext,
                            Intent(mContext, MainActivity::class.java)
                        )
//                        NightModeUtil.initNightMode(!night)//无效
                    }, 500)
                }.create().show()
        }
        //系统
        v.ll2.setOnClickListener {
            val sys = NightModeUtil.getSystemMode()

            CustomDialog.Builder(mContext).setView().setMessage("新的设置需要重启应用")
                .setCancelable(false).setPositiveButton { dialog: DialogInterface, _ ->
                    dialog.cancel()
                    v.ll1.visibility = if (sys) View.VISIBLE else View.GONE
                    v.tv2.text = if (sys) "否" else "是"
                    NightModeUtil.setSystemMode(!sys)
                    android.os.Handler(Looper.getMainLooper()).postDelayed({
                        LogUtil.e("getNightMode=" + NightModeUtil.getNightMode())
                        NightModeUtil.restartApp(
                            mContext,
                            Intent(mContext, MainActivity::class.java)
                        )
                    }, 500)
                }.create().show()
        }

        v.btnDefault.setOnClickListener {
            v.map!!["username1"] = "username111"
            v.user1!!["lastName"] = "lastName111"
            ++v.mCount
            AppTheme.update(Themes.Other)
            vm.getHots()
        }
        v.btnDay.setOnClickListener { AppTheme.update(Themes.Day) }
        v.btnNight.setOnClickListener { AppTheme.update(Themes.Night) }

        val cv = DataBindingUtil.inflate<PopupDemoBinding>(
            LayoutInflater.from(App.instance),
            R.layout.popup_demo,
            null,
            false
        )
        adapter = DemoAdapter(mContext, leftList)
        cv.rvPop.layoutManager = LinearLayoutManager(mContext)
        cv.rvPop.adapter = adapter
        val pop = PopupWindowFix(cv.root)
        pop.setDismiss(true)
        v.tvH.setOnClickListener { pop.showAsDropDown(v.tvH) }
    }

    override fun initData() {
        v.tv1.text = if (NightModeUtil.getNightMode()) {
            "是"
        } else {
            "否"
        }
        v.tv2.text = if (NightModeUtil.getSystemMode()) {
            "是"
        } else {
            "否"
        }
        if (NightModeUtil.getSystemMode()) v.ll1.visibility = View.GONE

        vm.getToken()
    }

    override fun lazyLoadData() {
        v.map = hashMapOf("username1" to "username", "password1" to "password")
        v.user1 = ObservableArrayMap<String, Any>().apply {
            put("firstName", "Google")
            put("lastName", "Inc.")
            put("age", 17)
        }
        v.user2 = ObservableArrayList<Any>().apply {
            add("Google")
            add("Inc.")
            add(17)
        }
//        v.list.maxByOrNull { it.length }
        v.index1 = 2
    }

}